package com.imageim.yimirror.ui.activity;

import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;

import com.imageim.yimirror.R;
import com.imageim.yimirror.base.BaseActivity;
import com.imageim.yimirror.bean.UploadPhotoResultBean;
import com.imageim.yimirror.common.data.CommonData;
import com.imageim.yimirror.common.network.NetUtil;
import com.imageim.yimirror.common.network.constants.NetConstants;
import com.imageim.yimirror.common.network.listener.NetCallback;
import com.imageim.yimirror.common.network.tool.ParamCreator;
import com.imageim.yimirror.common.ui.SceneManager;
import com.imageim.yimirror.common.ui.tip.TipUtil;
import com.imageim.yimirror.common.util.UserUtil;

import org.apache.http.message.BasicNameValuePair;

import java.util.List;

/**
 * 网络异常
 */
public class NoNetworkUploadPhotoActivity extends BaseActivity implements View.OnClickListener {

    private Button btnRefresh;
    private CountDownTimer timer;
    //    private Bitmap photoBitmap;
    //    private String photoStr;

    @Override
    protected void initBundle() {
        //        photoBitmap = getIntent().getParcelableExtra("photoBitmap");
        //        photoStr = getIntent().getStringExtra("photoStr");
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_no_network_upload_photo;
    }

    @Override
    protected void initView() {
        btnRefresh = findViewById(R.id.btn_refresh);
        btnRefresh.setOnClickListener(this);
        findViewById(R.id.btn_close).setOnClickListener(this);
    }

    @Override
    protected void initData() {
        timer = new CountDownTimer(61000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                btnRefresh.setText("刷新 " + millisUntilFinished / 1000 + "S");
            }

            @Override
            public void onFinish() {
                finish();
            }
        };
        timer.start();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_refresh:
                refreshUploadPhoto();
                break;
            case R.id.btn_close:
                finish();
                break;
            default:
        }
    }

    private void refreshUploadPhoto() {
        //        String photoStr = null;
        //        if (photoBitmap != null) {
        //            photoStr = BitmapUtils.bitmapToBase64(photoBitmap);
        //        } else {
        //            return;
        //        }
        if (CommonData.scanUserPhotoStr == null) {
            return;
        }
        List<BasicNameValuePair> params = new ParamCreator().add("userPhoto", CommonData.scanUserPhotoStr).build();
        NetUtil.postObject(this, NetConstants.URL.UPLOAD_SCAN_USER_PHOTO, params, new NetCallback<UploadPhotoResultBean>() {
            @Override
            public void onStart() {
                showLoading();
            }

            @Override
            public void onSuccess(UploadPhotoResultBean result) {
                hideLoading();
                if (result.getGender() == 2) { // female
                    UserUtil.setPhotoNo(result.getPhotoNo());
                    if (result.getUserId() != null) {
                        UserUtil.setUserId(result.getUserId() + "");
                    }
                    //保存用户使用次数、推荐编号用来获取推荐服装、新老用戶標簽、是否做過體驗報告測試題
//                    UserUtil.setUserFlag(result.getFlag());
//                    UserUtil.setRecommenNo(result.getRecommendNo());
//                    UserUtil.setUseNo(result.getUseNo());
//                    UserUtil.setExperienceReportFlag(result.getExperienceReportFlag());
                    SceneManager.toScene(mContext, MainActivity.class, null);
                } else { // male
                    SceneManager.toScene(mContext, GenderTipActivity.class, null);
                }
                CommonData.scanUserPhotoStr = null;
                finish();
            }

            @Override
            public void onFail(int errorCode, String errorMsg) {
                hideLoading();
                TipUtil.show(errorMsg + " " + errorCode);
            }
        }, UploadPhotoResultBean.class);
    } // refresh

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            timer.cancel();
        }
        //        if (photoBitmap != null && !photoBitmap.isRecycled()) {
        //            photoBitmap.recycle();
        //        }
    }
}
